package com.yaorange.module.system.service.impl;

import com.yaorange.module.system.mapper.SysUserRoleRepository;
import com.yaorange.module.system.service.SysUserRoleService;
import com.yaorange.module.system.service.SysUserService;
import com.yaorange.pojo.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Autowired
    private SysUserRoleRepository sysUserRoleRepository;
    @Autowired
    private SysUserService sysUserService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addRelation(Long userId, List<Long> roleIds) {
        if (roleIds.contains(1L)){
            sysUserService.grantRolesList(userId,true);
        }else {
            sysUserService.grantRolesList(userId,false);
        }
        sysUserRoleRepository.deleteRelationByUserId(userId);
        if(roleIds!=null && roleIds.size()>0) {
            sysUserRoleRepository.insertRelation(userId, roleIds);
        }
    }

    @Override
    public List<SysUserRole> findById(Integer id) {
        return sysUserRoleRepository.findById(id);
    }
}
